package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberCountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/count")
public class MemberCountController {

    @Reference
    private MemberCountService memberCountService;

    @RequestMapping("/getMemberCount")
    public Result getMemberCount() {

        List<Map<String, Object>> setmealCount = memberCountService.countByMember();
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : setmealCount) {

            if(String.valueOf(map.get("name")).equals("1")){
                map.put("name","男");
            }else {
                map.put("name","女");
            }
            setmealNames.add(String.valueOf(map.get("name")));
        }


        Map<String, Object> map = new HashMap<>();
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", setmealCount);


        return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    @RequestMapping("/getMemberAgeReport")
    public Result getMemberAgeReport() {
        try {

            List<Map<String, Object>> ageCount = memberCountService.findMemberByage();
            Map<String, Object> map = new HashMap<>();
            map.put("ageCount", ageCount);

            List<String> memberAge = new ArrayList<>();

            for (Map<String, Object> maplist : ageCount) {
                String name = (String) maplist.get("name");
                memberAge.add(name);
            }
            map.put("memberAge", memberAge);

            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
